﻿#include "StickyNote.h"
#include <QPainter>
#include "Tools.h"
#include <QClipboard>
#include <QFileDialog>
#include <QApplication>
#include <QCursor>
// add by liq 12
#include <QDateTime>
#include "common/Util.h"
// add by liq 15
#include "controller/WindowManager.h"

#include <QGraphicsDropShadowEffect>
#include <QLabel>
#include <QGridLayout>

// add by liq 15
static QString LAST_SAVE_PATH = "";

StickyNote::StickyNote(const QImage& image, const QRect& originScreenRect, const QString& lastSavePath, QWidget *parent) : QWidget(parent), image(image), originScreenRect(originScreenRect)
{
    setWindowFlags(Qt::ToolTip | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint);
    // 调整size+20是为了显示外边缘发光效果
    setFixedSize(image.size() + QSize(20, 20));
    setFocusPolicy(Qt::StrongFocus);

    show();
    activateWindow();
    setFocus();

    tools = new Tools;
    tools->setTack();

    initToolConnect();

    // add by liq 15
    LAST_SAVE_PATH = lastSavePath;

    //    setStyleSheet("background-color: transparent;");
    //    setStyleSheet("background-color: rgba(0, 0, 0, 0);");
    // 透明背景，以上设置均无效（显示黑色）
    setAttribute(Qt::WA_TranslucentBackground, true);
}

StickyNote::~StickyNote()
{
    delete tools;
}

QImage StickyNote::getScreenShotImage()
{
    return image;
}

void StickyNote::initToolConnect()
{
    connect(tools, &Tools::download, [this]{
        // modify by liq 15
//        QString writeDir = Util::getWritebaleDir();
        QString writeDir = LAST_SAVE_PATH;
        QString imgName = QString("yfscreenshot-") + QDateTime::currentDateTime().toString("hhmmss") + ".png";
        QString imgPath = writeDir + "/" + imgName;
        QString filename = QFileDialog::getSaveFileName(nullptr, QString::fromLocal8Bit("保存截图"), imgPath, QString::fromLocal8Bit("图片(*.png *.jpg *.jpeg *.gif)"));
        if(filename.isEmpty()) return;
        getScreenShotImage().save(filename);
        // add by liq 15
        emit WindowManager::instance()->sigUpdateLastSavePath(filename, true);
        // 需要更新LAST_SAVE_PATH
        QFileInfo fileinfo(filename);
        LAST_SAVE_PATH = fileinfo.absoluteFilePath();
    });
    connect(tools, &Tools::clipBoard, [this]{
        QClipboard* clipBoard = qApp->clipboard();
        clipBoard->setImage(getScreenShotImage());
    });
    connect(tools, &Tools::finished, this, &StickyNote::deleteLater);
}

void StickyNote::clearTimer()
{
    if(timerId != -1){
        killTimer(timerId);
        timerId = -1;
    }
    setFocus();
}

void StickyNote::paintEvent(QPaintEvent *)
{
//    QPainter painter(this);
//    painter.setPen(Qt::NoPen);
//    painter.drawImage(rect(), image);
    QPixmap pixmap = QPixmap::fromImage(image);  // 将QImage转换为QPixmap
    QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect;
    effect->setColor(QColor(32, 128, 240));  // 设置发光颜色
    effect->setBlurRadius(10);  // 设置模糊半径
    effect->setOffset(0);  // 设置偏移量

    QLabel* label = new QLabel(this);
    label->setPixmap(pixmap);

    QGridLayout* layout = new QGridLayout(this);
    layout->addWidget(label);
    layout->setContentsMargins(10,10,10,10);
    setLayout(layout);

    label->setGraphicsEffect(effect);
}

void StickyNote::mousePressEvent(QMouseEvent *)
{
    pressedPoint = QCursor::pos();
}

void StickyNote::mouseMoveEvent(QMouseEvent *)
{
    QPoint point = QCursor::pos();
    move(pos() - pressedPoint + point);
    pressedPoint = point;
    // 减掉图片边缘的发光区域
    tools->set(geometry()- QMargins(10,10,10,10), originScreenRect);
}

void StickyNote::mouseReleaseEvent(QMouseEvent *)
{
    // 减掉图片边缘的发光区域
    tools->set(geometry()- QMargins(10,10,10,10), originScreenRect);
}

void StickyNote::focusInEvent(QFocusEvent *)
{
    clearTimer();
}

void StickyNote::focusOutEvent(QFocusEvent *)
{
    timerId = startTimer(300);
}

void StickyNote::timerEvent(QTimerEvent *)
{
    tools->unset();
}
